package com.example.test.model;

import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.sohu.microblog.framework.net.task.CallBackManager;
import com.sohu.microblog.framework.net.task.TaskCallBack;
import com.sohu.microblog.http.blogapi.SohuApi;
import com.tencent.weibo.sdk.android.model.GlobalVar;

import android.R.layout;
import android.os.Handler;
import android.os.Message;

public class SohuWeiboAPI {

	static HashMap<Integer, Handler> handlerMap;
    private static String synStr="";
    private static List<HashMap<String, String>> lArrData;
	private static TaskCallBack callback = new TaskCallBack(){
		
		@Override
		public void onGetCount(int id, String data) {
			try {
				JSONObject jsonObject=new JSONObject(data);
				lArrData.get(0).put("comments_count", jsonObject.getString("comments_count"));
				lArrData.get(0).put("reposts_count", jsonObject.getString("transmit_count"));
				lArrData.get(0).put("attitudes_count", "0");
                sendMsg(handlerMap.get(id), Constants.MSG_GETONEWEIBO_SUCCESS, lArrData);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		@Override
		public void onGetCommentToMeLine(int id, String data) {
			if(data.equals("[]")) 
				return ;
			List<HashMap<String, String>> lArrData = NetJsonAnalyzer.JsonToHasArr(data,"comments");
			 for (HashMap<String, String> map : lArrData) {
				 map.put("statususername", map.get("in_reply_to_screen_name"));
				 map.put("statustext", map.get("in_reply_to_status_text"));
				 map.put("statusid", map.get("in_reply_to_status_id"));
				 map.put("resource", "sohu");
	               if(map.get("source").indexOf("</a>")==-1)
	               {
	            	   map.put("source", "<a>"+map.get("source")+"</a>");
	               }
			}
			sendMsg(handlerMap.get(id), Constants.MSG_GETCOMMENT_SUCCESS, lArrData);
		}
		@Override
		public void onGetMentionLine(int id, String data) {
			if(data.equals("[]"))
				return;
			List<HashMap<String, String>> lArrData = SohuJsonAnalyzer.JsonToHasArr("{status:"+data+"}","status");
			 for (HashMap<String, String> map : lArrData) {
	               map.put("reposts_count","0");
	               map.put("comments_count","0");
	               map.put("attitudes_count","0");
	               if(!map.get("in_reply_to_status_id").equals(""))
	               {
	            	   map.put("retweeted_statustext",map.get("in_reply_to_status_text"));
	                   map.put("retweeted_statusid",map.get("in_reply_to_status_id"));
	                   map.put("retweeted_statususerscreen_name",map.get("in_reply_to_screen_name"));
	               }
	               if(map.get("source").indexOf("</a>")==-1)
	               {
	            	   map.put("source", "<a>"+map.get("source")+"</a>");
	               }
	               String pics=map.get("small_pic");
	               if(pics!=null)
	               map.put("images", map.get("small_pic"));
	               else
	               map.put("images", "");   
	               map.put("retweeted_statusimages", "");
			}
			sendMsg(handlerMap.get(id), Constants.MSG_GETTIMELINE_SUCCESS, lArrData);
		}
		@Override
		public void onGetOneWeibo(int id, String data) {
			lArrData= NetJsonAnalyzer.JsonToHasArr("{status:["+data+"]}","status");
			SohuWeiboAPI.getCount(lArrData.get(0).get("id"),handlerMap.get(id)); 
			for (HashMap<String, String> map : lArrData) {
	             
	               if(map.get("source").indexOf("</a>")==-1)
	               {
	            	   map.put("source", "<a>"+map.get("source")+"</a>");
	               }
	               String pics=map.get("original_pic");
	               if(pics!=null)
	               map.put("images", map.get("original_pic"));
	               else
	               map.put("images", "");   
	               if(!map.get("in_reply_to_status_id").equals(""))
	               {
	            	   map.put("retweeted_statustext",map.get("in_reply_to_status_text"));
	                   map.put("retweeted_statusid",map.get("in_reply_to_status_id"));
	                   map.put("retweeted_statususerscreen_name",map.get("in_reply_to_screen_name"));
	               }
	               map.put("retweeted_statusimages", "");   
			}
		}
		@Override
		public void onGetUserTimeLine(int id, String data) {
			onGetTimeline(id, data);
		}
		
		@Override
		public void onDeleteComment(int id, String data) {
			sendMsg(handlerMap.get(id), Constants.MSG_DELETECOMMENT_SUCCESS, null);
		}
		@Override
		public void onComment(int id, String data) {
			try {
				sendMsg(handlerMap.get(id), Constants.MSG_COMMENT_SUCCESS, "sohu"+new JSONObject(data).getString("id"));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		@Override
		public void onRepost(int id, String data) {
			try {
				sendMsg(handlerMap.get(id), Constants.MSG_REPOST_SUCCESS, "sohu"+new JSONObject(data).getString("id"));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		@Override
		public void onGetUserTimeLineError(int id, String err) {
			// TODO Auto-generated method stub
			super.onGetUserTimeLineError(id, err);
		}
		@Override
		public void onGetCommentList(int id, String data) {
			List<HashMap<String, String>> lArrData = NetJsonAnalyzer.JsonToHasArr("{comment:"+data+"}","comment");
			
			sendMsg(handlerMap.get(id), Constants.MSG_GETCOMMENTLIST_SUCCESS, lArrData);
		}
		@Override
		public void onGetRepostsList(int id, String data) {
			List<HashMap<String, String>> lArrData = NetJsonAnalyzer.JsonToHasArr("{reposts:"+data+"}","reposts");
			for (HashMap<String, String> map : lArrData) {
				map.put("userscreen_name", map.get("username"));
			}
			sendMsg(handlerMap.get(id), Constants.MSG_GETREPOSTLIST_SUCCESS, lArrData);
		}
		public void onGetAccountInfo(int id, String data) {
			List<HashMap<String, String>> lArrData = NetJsonAnalyzer.JsonToHasArr("{user:["+data+"]}","user");
			lArrData.get(0).put("avatar_large", lArrData.get(0).get("profile_image_url"));
			lArrData.get(0).put("name", lArrData.get(0).get("screen_name"));
			sendMsg(handlerMap.get(id), Constants.MSG_GETUSERINFO_SUCCESS, lArrData);
		};
		/*public void onGetAccountInfoError(int id, String err) {
			Toast.makeText(MainActivity.this, err, Toast.LENGTH_SHORT).show();
		};*/
		public void onGetTimeline(int id, String data) {
			
			
			List<HashMap<String, String>> lArrData = SohuJsonAnalyzer.JsonToHasArr("{status:"+data+"}","status");
			 for (HashMap<String, String> map : lArrData) {
	               map.put("reposts_count","0");
	               map.put("comments_count","0");
	               map.put("attitudes_count","0");
	               if(!map.get("in_reply_to_status_id").equals(""))
	               {
	            	   map.put("retweeted_statustext",map.get("in_reply_to_status_text"));
	                   map.put("retweeted_statusid",map.get("in_reply_to_status_id"));
	                   map.put("retweeted_statususerscreen_name",map.get("in_reply_to_screen_name"));
	               }
	               if(map.get("source").indexOf("</a>")==-1)
	               {
	            	   map.put("source", "<a>"+map.get("source")+"</a>");
	               }
	               String pics=map.get("small_pic");
	               if(pics!=null)
	               map.put("images", map.get("small_pic"));
	               else
	               map.put("images", "");   
	               map.put("retweeted_statusimages", "");
			}
			sendMsg(handlerMap.get(id), Constants.MSG_GETTIMELINE_SUCCESS, lArrData);
		};
		public void onGetPublic(int id, String data) {
			onGetTimeline(id, data);
		};
		/*public void onGetTimelineError(int id, String err) {
			Toast.makeText(MainActivity.this, err, Toast.LENGTH_SHORT).show();
		};
		public void onSendblog(int id, String data) {
			content.setText(data);
		};
		public void onSendblogError(int id, String err) {
			Toast.makeText(MainActivity.this, err, Toast.LENGTH_SHORT).show();
		};
		
		public void onGetPublicError(int id, String err) {
			Toast.makeText(MainActivity.this, err, Toast.LENGTH_SHORT).show();
		};*/
	};
	static
	{
		CallBackManager.addListener(callback);
		handlerMap =new HashMap<Integer, Handler>();
	}
	 //获取单条微博资料
	public static void  getOneWeibo(String weiboId,final Handler handler)
	{
		if(!GlobalVar.enWhich.equals("all")&&!GlobalVar.enWhich.equals("sohu"))
			return;
		int id=SohuApi.getInstance().doGetOneWeibo(weiboId);
		handlerMap.put(id, handler);
		id=SohuApi.getInstance().doGetCommentList(weiboId);
		handlerMap.put(id, handler);
	}
	 //获取单条微博资料
		public static void  getCount(String weiboId,Handler handle)
		{
			if(!GlobalVar.enWhich.equals("all")&&!GlobalVar.enWhich.equals("sohu"))
				return;
			int id= SohuApi.getInstance().doGetCount(weiboId);
			handlerMap.put(id, handle);
		}
		 //获取单条评论
		public static void  deleteComment(String weiboId,Handler handle)
		{
			if(!GlobalVar.enWhich.equals("all")&&!GlobalVar.enWhich.equals("sohu"))
				return;
			int id= SohuApi.getInstance().doDeleteComment(weiboId);
			handlerMap.put(id, handle);
		}
	 //获取userline
		public static void  getUserline(String uid,String name,final Handler handler,String maxId,int count)
		{
			if(!GlobalVar.enWhich.equals("all")&&!GlobalVar.enWhich.equals("sohu"))
				return;
			int id=SohuApi.getInstance().doGetUserTimeline(uid,name,maxId,count);
			handlerMap.put(id, handler);
		}

	// 获取userinfo
	public static void getUserInfo(String uid,String name, final Handler handler) {
		if(!GlobalVar.enWhich.equals("all")&&!GlobalVar.enWhich.equals("sohu"))
			return;
		int id = SohuApi.getInstance().doGetAccountInfo(uid,name);
		handlerMap.put(id, handler);
	}
	//发表微博
	public static void sendWeibo(String content)
	{
		if(!GlobalVar.enWhich.equals("all")&&!GlobalVar.enWhich.equals("sohu"))
			return;
		SohuApi.getInstance().doSendblog(content);
	}
	//评论一条微博
	public static void comment(String content,String id ,Handler handler)
	{
		if(!GlobalVar.enWhich.equals("all")&&!GlobalVar.enWhich.equals("sohu"))
			return;
		int rId = SohuApi.getInstance().doComment(content, id);
		handlerMap.put(rId, handler);
	}
	//转发一条微博
		public static void repost(String content,String id ,Handler handler)
		{
			if(!GlobalVar.enWhich.equals("all")&&!GlobalVar.enWhich.equals("sohu"))
				return;
			int rId = SohuApi.getInstance().doRepost(content, id);
			handlerMap.put(rId, handler);
		}
	//获取朋友时间线
	public static void  getFriendline(final Handler handler,String maxId,int count)
	{
		if(!GlobalVar.enWhich.equals("all")&&!GlobalVar.enWhich.equals("sohu"))
			return;
		Integer id=SohuApi.getInstance().doGetTimeline(maxId,count);

		handlerMap.put(id, handler);
	}
	//获取评论我的
		public static void  getCommentToMe(final Handler handler,String maxId,int count)
		{
			if(!GlobalVar.enWhich.equals("all")&&!GlobalVar.enWhich.equals("sohu"))
				return;
			Integer id=SohuApi.getInstance().doGetCommentToMe(maxId,count);

			handlerMap.put(id, handler);
		}
	
	//获取@我的
	public static void getMentionLine(Handler handler,String maxId,int count)
	{
		if(!GlobalVar.enWhich.equals("all")&&!GlobalVar.enWhich.equals("sohu"))
			return;
		int id = SohuApi.getInstance().doGetMentionLine(maxId,count);
		handlerMap.put(id, handler);
	}
	//获取公共line
		public static void getPubLine(Handler handler,String maxId,int count)
		{
			if(!GlobalVar.enWhich.equals("all")&&!GlobalVar.enWhich.equals("sohu"))
				return;
			int id = SohuApi.getInstance().doGetPublicBlog(maxId,count);
			handlerMap.put(id, handler);
		}
	//转发微博
	
	public static  void sendMsg(Handler handler,int msgType,Object obj)
 	{
		
 		    Message  msg =new Message();
			msg.what=msgType;
			msg.obj=obj;
			handler.sendMessage(msg);
 	}
	
}
